<?php
/**
 * 21天减重测评算法
 * Created by PhpStorm.
 * User: liao
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;
use ParamConfig;

use App\Services\Result\BaseResult;

use App\Models\LogHelper;

class TraditionalMedicineResult extends BaseResult implements Resultable
{
    private $answers;
    /**肥胖指数*/
    private $totalSummaryAttrObj;
    /**身体概述*/
    private $bodyFeatureAttrObj;
    /**健康指数*/
    private $advicesAttrObj;


    private $maxValueIndexes ;
    private $maxValueSRObj ;

    public function __construct($answersStr = null)
    {
        $this->answers = $this->convertAnswer($answersStr);
        $this->totalSummaryAttrObj = $this->configAttrObj(1,'总体概况');
        $this->bodyFeatureAttrObj = $this->configAttrObj(2,'体质特征');
        $this->advicesAttrObj = $this->configAttrObj(3,'调养建议');
        //最大值组成的数组索引
        $this->maxValueIndexes = [63,64,65,66,67,68,69,70,71];
        //要替换的最大值对应的数组obj
        $this->maxValueSRObj = $this->getMaxIndexSearchReplaceValue();
    }

    public function result()
    {
        $attrs = [];
        /*减肥指数*/
        array_push($attrs,$this->totalSummaryAttr());
        /*身材概述*/
        array_push($attrs,$this->bodyFeatureAttr());
//        /**健康指数*/
        array_push($attrs,$this->advicesAttrObj());

        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型为雷达图
        $data['chatType'] = $this->getChatType('radar');
        return $data;

    }


    /**
     * 肥胖指数
     */
    protected function totalSummaryAttr(){
        $note = '';
        $this->getNoteByTab($note,'total_summary');
        $obj = $this->totalSummaryAttrObj;
        $obj = $this->compactAttrObj($obj['attr_id'],$obj['title'],$note);
        LogHelper::log($obj);
        return $obj;
    }


    /**
     * 身材概述
     */
    protected function bodyFeatureAttr(){
        $note = '';
        $this->getNoteByTab($note,'body_feature');
        $obj = $this->bodyFeatureAttrObj;
        $obj = $this->compactAttrObj($obj['attr_id'],$obj['title'],$note);
        return $obj;
    }



    /**
     * 健康指数
     */
    protected function advicesAttrObj(){
        $note = '';
        $this->getNoteByTab($note,'advice');
        $obj = $this->advicesAttrObj;
        $obj = $this->compactAttrObj($obj['attr_id'],$obj['title'],$note);
        return $obj;
    }


    /**
     * 根据id和tagkey获取对应的配置的值
     */
    protected function getNoteByTab(&$note,$tabKey){
        $note .= $this->getTabResult($tabKey);
    }



    protected function getSingleValue($tagKey, $index){
        $tag = $this->getResultTag($tagKey);
        $values = ParamConfig::getByTag($tag);
        $answer = $this->getAnswerObjByIndex($index);
        $replaceKey = '@'.$index;
        $result = '';
        foreach($values as $d){
            if(substr($d['key'],0,3) == $replaceKey){
                $expression = str_replace($replaceKey,$answer['weight'],$d['key']);
                if($this->getQuestionItemCondResult($expression,$answer['weight'])){
                    $result = $d['value'];
                    break;
                }
            }
        }
        return $result;
    }


    /**
     * 获取条件结果集对应的tag
     */
    protected function getResultTag($tagKey){
        $keys = $this->resultConditionKeys();
        return $keys[$tagKey]?$keys[$tagKey]:'';
    }


    /**
     * 所有tab在数据库中对应的key
     */
    protected function resultConditionKeys(){
        return [
            'total_summary' => 'TD.total.summary.condition',
            'body_feature' => 'TD.body.feature.condition',
            'advice' => 'TD.advice.condition',
        ];
    }




    protected function getMaxIndexSearchReplaceValue(){
        $search = [];
        $replace = [];
        foreach ($this->maxValueIndexes as $index){
            $answer = $this->getAnswerObjByIndex($this->answers,$index);
            array_push($search,'@'.$index);
            array_push($replace,$answer['weight']);
        }
        return [
            'search' => $search,
            'replace' => $replace
        ];
    }


    /**
     * 获取tab对应的值
     * @param $tagKey 单个tab在param_config表中对应的tag，详细见resultConditionKeys方法中定义的
     * @return array
     */
    protected function getTabResult($tagKey){
        $result = '';
        $tag = $this->getResultTag($tagKey);
        //最大值中要替换的数组对象
        $maxValueReplaceObj = $this->maxValueSRObj;
        if($values = ParamConfig::getByTag($tag)){
            foreach ($this->maxValueIndexes as $index){
                $searchIndex = '@'.$index;
                foreach ($values as $data){
                    $key = $data['key'];
                    $subIndexStr = substr($key,0,3);
                    $expression = str_replace($maxValueReplaceObj['search'],$maxValueReplaceObj['replace'],$key);
                    //后面的参数1可以任意，只是一个占位符
                    if($subIndexStr == $searchIndex && $this->getQuestionItemCondResult($expression,1)){
                        $result = $data['value'];
                        break;
                    }
                }
            }
        }
        return $result;
    }



    protected function getChatparam(){
        $chatdata = [];
        $mapping = $this->getFeatureMapping();
        foreach ($this->maxValueIndexes as $index){
            $answer = $this->getAnswerObjByIndex($this->answers,$index);
            $obj['key'] = $mapping[$index];
            $obj['value'] = round(doubleval($answer['weight']),2);
            array_push($chatdata,$obj);
        }
        return $chatdata;
    }

    private function getFeatureMapping(){
        return [
            63 => '阳虚质',
            64 => '阴虚质',
            65 => '气虚质',
            66 => '痰湿质',
            67 => '湿热质',
            68 => '血瘀质',
            69 => '特禀质',
            70 => '气郁质',
            71 => '平和质',
        ];

    }




}